<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Bill\Controllers;


use App\Eloquent\Ygt\BillLog;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\SellerBill;
use App\Eloquent\Ygt\SellerCompany;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class SellerController extends Controller
{
    public $userId          = 25;
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $type               = request('type',2);//0:未结清 1:已结清客户,2全部客户
        $page               = request( 'page', 1 );
        $limit              = request('limit', 10 );
        $offset             = ($page - 1) * $limit;
        if( !$userInfo )
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = [
            'company_id'=>$companyId,
            'type'=>1,
        ];
        if($type == 1)
        {
            $where['status']    = 1;
        }
        else if($type == 0){
            $where['status']    = 0;
        }
        $fields             = 'seller_company_id,sum(seller_company_id) as now_money_sum';
        $orderby            = 'now_money_sum';
        $groupby            = 'seller_company_id';
        $object             = SellerBill::where($where)->selectRaw($fields)->groupBy($groupby);
        if($type ==1){
            $object = $object->having('now_money_sum','=',0);
        }
        else if($type == 0){
            $object = $object->having('now_money_sum','>',0);
        }

        $data               = $object->limit($limit+1)->offset($offset)->orderByDesc($orderby)->get();

        $obj                = SellerBill::where($where);
        $obj                = $obj->selectRaw('COUNT(DISTINCT seller_company_id) AS num');
        $countStd           = $obj->first();
        $count              = $countStd->num;
        $idArr              = [];
        foreach($data as $key=>$val)
        {
            $idArr[]        = $val['seller_company_id'];
        }
        $listData               = [];
        if(!empty($idArr))
        {
            //取头像和名字
            $where              = ['id'=>['in',$idArr]];
            $fields             = 'id,title,img_id,contact_mobile';
            $lists              = SellerCompany::getList($where,$fields)->toArray();
            foreach($lists as $key=>$val)
            {
                $temp               = $val;
                $imgPath            = Func::getImgUrlById($val['img_id']);
                $temp['img_path']  = $imgPath;
                $temp['last_money_text']  = '';
                $temp['last_order_text']  = '';
                $temp['income_money']     = '';
                $temp['status']     = 0;
                $listData[]         = $temp;
            }
            //取最近收款
            //取最近订单
            //取应收款
            $fields             = 'seller_company_id,sum(now_money) as now_money_sum';
            $groupby            = 'seller_company_id';
            $object             = SellerBill::whereIn('seller_company_id',$idArr);
            $object             = $object->where('company_id','=',$companyId);
            $object             = $object->where('status','=',0);
            $object             = $object->where('type','=',1);
            $moneyList          = $object->selectRaw($fields)->groupBy($groupby)->get();
            $moneyListArr       = [];
            foreach($moneyList as $key=>$val)
            {
                $valCustomerId  = $val['seller_company_id'];
                $valMoneySum    = $val['now_money_sum'];
                $moneyListArr[$valCustomerId] = $valMoneySum;
            }
            foreach($listData as $key=>$val)
            {
                $valId          = $val['id'];
                $incomeMoney    = 0;
                if(isset($moneyListArr[$valId]))
                {
                    $incomeMoney    = $moneyListArr[$valId];
                }
                $listData[$key]['income_money'] = $incomeMoney;
            }
        }
        $where              = ['company_id'=>$companyId,'status'=>0];
        $totalMoney         = SellerBill::where($where)->sum('now_money');
        $data               = [
            'lists'=>$listData,'total_money'=>$totalMoney
        ];
        $pagingData         = [
            'page'         => $page,
            'pageSize'     => $limit,
            'total'         => $count,
            'total_page'   => ceil($count/$limit),
            'keyword'      => ''
        ];
        return $this->pagingData($data, $pagingData);
    }
    public function billList()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $type               = request('type',2);//0:未结清 1已结清客户,2全部客户
        $page               = request('page',1);
        $limit              = request('limit',10);
        $sellerCompanyId    = request('seller_company_id',0);

        if(!$userInfo)
        {
            xThrow( ERR_PARAMETER,'登录超时' );
        }
        $companyId          = $userInfo->company_id;
        $where              = [
            'ygt_seller_bill.company_id'=>$companyId,
            'ygt_seller_bill.type'=>1,
        ];
        if($sellerCompanyId){
            $where[ 'ygt_seller_bill.seller_company_id'] = $sellerCompanyId;
        }
        if($type == 1)
        {
            $where['status']    = 1;
        }
        else if($type == 0){
            $where['status']    = 0;
        }
        $lists              = SellerBill::getJoinListByType($where)->toArray();
        $count              = SellerBill::getCount($where);
        foreach($lists as $key=>$val )
        {
            $statusName         = $val['status'] == 1 ? '已结清' : '未结清';
            $lists[$key]['title'] = $val['seller_company_name'];
            $lists[$key]['status_name'] = $statusName;
            $lists[$key]['img_path']     = '';
            $lists[$key]['money']     = $val['total_money'];
            $lists[$key]['created_at']     = date('Y-m-d',$val['created_at']);
            $lists[$key]['created_at_ymd']     = date('Y-m-d',$val['created_at']);
            $lists[$key]['pre_money']     = '';
        }
        $where              = [
            'company_id'=>$companyId,
            'status'=>0
        ];
        $totalMoney         = SellerBill::where($where)->sum('now_money');
        $listData           = [
            'lists'=>$lists,
            'total_money'=>$totalMoney
        ];
        $pagingData         = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($listData, $pagingData);
    }
    public function moneyList()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $tableId            = request( 'id', 0 );
        if( !$userInfo || !$tableId )
        {
            xThrow( ERR_PARAMETER );
        }
        $where              = [
            'ygt_bill_log.type'=>3,
            'ygt_bill_log.table'=>'seller_bill',
            'ygt_bill_log.table_id'=>$tableId
        ];
        $limit              = $offset = '';
        $lists              = BillLog::getListDetail($where,$limit,$offset);
        return $lists;
    }
}
